﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Security;

namespace GoogleAppsTools
{
    public partial class SettingsForm : Form
    {
        bool restartRequired = false;

        public SettingsForm()
        {
            InitializeComponent();
            SetControls();

        }

        private void SetControls()
        {
            //populate form controls
            //authentication settings
            usernameTextBox.Text = EncryptionClass.ToInsecureString(EncryptionClass.DecryptString(Properties.Settings.Default.username));
            passwordTextBox.Text = EncryptionClass.ToInsecureString(EncryptionClass.DecryptString(Properties.Settings.Default.password));

            //calendar settings
            CalendarCheckSwitch(Properties.Settings.Default.CheckCalendar);
            RefreshRateUpDown.Value = Properties.Settings.Default.CalendarRefreshRate;
            getEventsUpDown.Value = Properties.Settings.Default.GetNewEventsTime;
            showPastEventsCheckBox.Checked = Properties.Settings.Default.ShowPastEvents;
            CheckForNewMailAndEventsCheckBox.Checked = Properties.Settings.Default.CheckForNewMailAndEvents;
            RefreshBrower.Checked = Properties.Settings.Default.RefreshBrowser;
            if (RefreshBrower.Checked)
            {
                RefreshRateUpDown.Enabled = true;
                refreshRateLabel.Enabled = true;
            }
            else
            {
                RefreshRateUpDown.Enabled = false;
                refreshRateLabel.Enabled = false;
            }
            //mail settings
            checkMailUpDown.Value = Properties.Settings.Default.CheckNewMailTime;
            playSoundCheckBox.Checked = Properties.Settings.Default.NewMailPlaySound;
        }

        private void OkButton_Click(object sender, EventArgs e)
        {
            //authentication settings
            Properties.Settings.Default.username = EncryptionClass.EncryptString(EncryptionClass.ToSecureString(usernameTextBox.Text));
            Properties.Settings.Default.password = EncryptionClass.EncryptString(EncryptionClass.ToSecureString(passwordTextBox.Text));

            //calendar settings
            Properties.Settings.Default.CheckCalendar = checkCalendarCheckBox.Checked;
            Properties.Settings.Default.CalendarRefreshRate = (int)RefreshRateUpDown.Value;
            Properties.Settings.Default.GetNewEventsTime = (int)getEventsUpDown.Value;
            Properties.Settings.Default.ShowPastEvents = showPastEventsCheckBox.Checked;
            Properties.Settings.Default.CheckForNewMailAndEvents = CheckForNewMailAndEventsCheckBox.Checked;
            Properties.Settings.Default.RefreshBrowser = RefreshBrower.Checked;

            //mail settings
            Properties.Settings.Default.CheckNewMailTime = (int)checkMailUpDown.Value;
            Properties.Settings.Default.NewMailPlaySound = playSoundCheckBox.Checked;

            //save settings
            Properties.Settings.Default.Save();
            if (restartRequired)
                MessageBox.Show("Please restart the application for the settings to take effect.");

            this.Close();
        }

        private void CancelFormButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void Authenticate()
        {
            tickPictureBox.Visible = false;

            if (usernameTextBox.Text != "" && passwordTextBox.Text != "")
            {
                string strPassword = passwordTextBox.Text;
                char[] passwordChars = strPassword.ToCharArray();
                SecureString password = new SecureString();
                foreach (char c in passwordChars)
                {
                    password.AppendChar(c);
                }

                if (GoogleMailChecker.CheckForNewMail(usernameTextBox.Text, password) != null)
                {
                    tickPictureBox.Image = GoogleCalendarApp.green_tick.ToBitmap();
                    tickPictureBox.Visible = true;
                }
                else
                {
                    tickPictureBox.Image = GoogleCalendarApp.Letter_X_icon.ToBitmap();
                    tickPictureBox.Visible = true;
                }
            }
        }

        private void SettingsForm_Load(object sender, EventArgs e)
        {
            restartRequired = false;
            tickPictureBox.Visible = false;
            SetControls();
            Authenticate();
        }

        private void usernameTextBox_Leave(object sender, EventArgs e)
        {
            Authenticate();
        }

        private void passwordTextBox_Leave(object sender, EventArgs e)
        {
            Authenticate();
        }

        private void RefreshBrower_CheckedChanged(object sender, EventArgs e)
        {
            if (RefreshBrower.Checked)
            {
                RefreshRateUpDown.Enabled = true;
                refreshRateLabel.Enabled = true;
            }
            else
            {
                RefreshRateUpDown.Enabled = false;
                refreshRateLabel.Enabled = false;
            }

        }

        private void checkCalendarCheckBox_CheckedChanged(object sender, EventArgs e)
        {
            restartRequired = true;
            CalendarCheckSwitch(checkCalendarCheckBox.Checked);
        }

        private void CalendarCheckSwitch(bool checkCalendar)
        {
            if (checkCalendar == true)
            {
                checkCalendarCheckBox.Checked = true;
                getNewEventsLabel.Enabled = true;
                getEventsUpDown.Enabled = true;
                showPastEventsCheckBox.Enabled = true;
                CheckForNewMailAndEventsCheckBox.Enabled = true;
            }
            if (checkCalendar == false)
            {
                checkCalendarCheckBox.Checked = false;
                getNewEventsLabel.Enabled = false;
                getEventsUpDown.Enabled = false;
                showPastEventsCheckBox.Enabled = false;
                CheckForNewMailAndEventsCheckBox.Enabled = false;
            }
        }
    }
}
